//
//  LYNetworking.m
//  礼物go
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LYNetworking.h"
#import <AFNetworking.h>
#import "Reachability.h"

@implementation LYNetworking

+(void)ly_GetWithURL:(NSString *)urlStr Parameter:(NSDictionary *)dic success:(successBlock)sucblock error:(failureBlock)errorBlock{
    //逻辑:
    //1.从本地取数据,显示在界面上
    //2.再从网络获取数据
    //a.如果有网显示最新数据
    //b.没网提示用户"网络不给力"
    if ([self checkNetworkingStatues]) {
        NSString *encodString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //设置请求类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript", nil]];
        [manager GET:encodString parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           
             // 将最新的数据存入本地数据库
            NSString *path = [NSString stringWithFormat:@"%ld.plist",[urlStr hash]];
            //存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            //归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            
            sucblock(responseObject);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
             errorBlock(error);
        }];
    } else{
    // 从本地数据库取数据
        NSString *path = [NSString stringWithFormat:@"%ld.plist",[urlStr hash]];
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        if (responseObject) {
            sucblock(responseObject);
        
        }
        
    }
}

+(void)ly_POSTWithURL:(NSString *)urlStr Parameter:(NSDictionary *)dic success:(successBlock)sucblock error:(failureBlock)errorBlock{
    if ([self checkNetworkingStatues]) {
      NSString *encodeString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript", nil]];
        [manager POST:encodeString parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            // 将最新的数据存入本地数据库
            NSString *path = [NSString stringWithFormat:@"%ld.plist",[urlStr hash]];
            //存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            //归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            
            sucblock(responseObject);

            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            errorBlock(error);
            
        }];
    } else{
        // 从本地数据库取数据
        NSString *path = [NSString stringWithFormat:@"%ld.plist",[urlStr hash]];
        
        NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        id responseObject = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
        if (responseObject) {
            sucblock(responseObject);
           
        }
  
    }
}



//检查网络状况
+(BOOL)checkNetworkingStatues{
 Reachability *reachability = [Reachability reachabilityForInternetConnection];
    if (reachability.currentReachabilityStatus == NotReachable) {
        return NO;
    }else {
        return YES;
    }
}


@end
